package com.lanou.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lanou.bean.Role;
import com.lanou.mapper.ModuleMapper;
import com.lanou.mapper.RoleMapper;
import com.lanou.service.RoleService;
import com.lanou.utils.AjaxResult;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by dllo on 18/3/12.
 */
@Service
public class RoleServiceImpl implements RoleService {
    @Resource
    private RoleMapper roleMapper;

    @Resource
    private ModuleMapper moduleMapper;

    @Override
    public List<Role> role(String username) {
        return roleMapper.findRoleByAdminId(username);
    }

    @Override
    public List<Role> allRole() {
        return roleMapper.selectAllRole();
    }



    @Override
    public void addrole(String name, List<Integer> modules) {
        /*int id = (int) ((Math.random() * 9 + 1) * 100);
        String roleid = String.valueOf(id);
        int roleId = Integer.parseInt(roleid);*/
        Role role = new Role();
        role.setName(name);
        roleMapper.addRole(role);
        int roleId = role.getRoleId();
        for (Integer module : modules) {
            roleMapper.addModules(roleId, module);
        }
    }

    @Override
    public void updata(Integer roleId, String name, List<Integer> modules) {
        roleMapper.updateRole(roleId, name);
        moduleMapper.delectMoule(roleId);
        for (Integer module : modules) {
            roleMapper.addModules(roleId, module);
        }
    }

    @Override
    public PageInfo<Role> queryRoleByPage(Integer pageNo, Integer pageSize) {
        pageNo = pageNo == null ? 1 : pageNo;
        pageSize = pageSize == null ? 3 : pageSize;
        PageHelper.startPage(pageNo, pageSize);
        List<Role> roleList = allRole();
        PageInfo<Role> rolePageInfo = new PageInfo<Role>(roleList);
        return rolePageInfo;
    }

    @Override
    public void delete(Integer roleId) {
        roleMapper.deleteRole(roleId);
        moduleMapper.delectMoule(roleId);
    }

}
